<template>
  <div class="app-container">
    <el-card class="box-card">
      <div class="clearfix" slot="header">
        <span>百度网盘授权api</span>
      </div>
      <div class="api">
        <el-button type="primary" icon="el-icon-share" @click="btnCode"
          >授权百度网盘</el-button
        >
      </div>
      <!-- 显示token -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="date" label="用户名" width="180">
        </el-table-column>
        <el-table-column prop="name" label="token"> </el-table-column>
        <el-table-column prop="address" label="操作" width="180">
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      tableData: [
        // {
        //   date: "2016-05-02",
        //   name: "王小虎",
        //   address: "上海市普陀区金沙江路 1518 弄",
        // },
        // {
        //   date: "2016-05-04",
        //   name: "王小虎",
        //   address: "上海市普陀区金沙江路 1517 弄",
        // },
        // {
        //   date: "2016-05-01",
        //   name: "王小虎",
        //   address: "上海市普陀区金沙江路 1519 弄",
        // },
        // {
        //   date: "2016-05-03",
        //   name: "王小虎",
        //   address: "上海市普陀区金沙江路 1516 弄",
        // },
      ],
    };
  },
  created() {
    const code = window.location.search;
    console.log(55, code);
    if (code) {
      // client_id 为AppKey
      const client_id = "ekYqhi3sG5SOA1RwGkln5gMAuIxjAfSM";
      // client_secret 为SecretKey
      const client_secret = "T2kz55B45XRsCNjNM4yKOWJJ9rq82E8A";
      // redirect_uri 回调地址
      const redirect_uri = "http://localhost:9529/example/authorization/";
      axios({
        url: `https://openapi.baidu.com/wpapi/oauth/2.0/token?grant_type=authorization_code&code=${zcode}&client_id=${client_id}&client_secret=${client_secret}&redirect_uri=${redirect_uri}`,
      }).then((res) => {
        console.log(res);
      });
    }
  },
  methods: {
    btnCode() {
      // 授权相关业务
      const client_id = "ekYqhi3sG5SOA1RwGkln5gMAuIxjAfSM";
      const redirect_uri = "http://localhost:9529/";
      const device_id = 88187203;
      const force_login = 1;
      window.location.href = `https://openapi.baidu.com/oauth/2.0/authorize?response_type=code&client_id=${client_id}&redirect_uri=${redirect_uri}example/authorization/&scope=basic,netdisk&device_id=${device_id}&force_login=${force_login}`;
    },
  },
};
</script>

<style scoped>
.api {
  padding: 0px 0;
}
</style>
